package cn.bzc.data.process.service.impl;

import cn.bzc.data.process.listener.PmBillDataListener;
import cn.bzc.data.process.model.entity.CustData;
import cn.bzc.data.process.model.entity.PmBillExcelData;
import cn.bzc.data.process.model.mapper.TableMapper;
import cn.bzc.data.process.model.mapper.TableMapper2;
import cn.bzc.data.process.service.DataProcessService;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.fastjson.JSON;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.io.File;
import java.sql.SQLOutput;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DataProcessServiceImpl implements DataProcessService {
    @Resource
    private TableMapper tableMapper;
    @Resource
    private TableMapper2 tableMapper2;

    @Override
    public void getTableList() {
        List<String> tableNameList1 = getTableNameList(tableMapper.listTable());
        List<String> tableNameList2 = getTableNameList(tableMapper2.listTable());
        List<String> errorTable = new ArrayList<>();
        HashMap<String, Long> errorMap = new HashMap<>();
        if(tableNameList2.size() == tableNameList1.size()) {
            for (int i = 0; i < Math.max(tableNameList1.size(),tableNameList2.size()); i++) {
                Map map1 = tableMapper.listTableValue(tableNameList1.get(i));
                long count1 = (long)map1.get("count");
                Map map2 = tableMapper2.listTableValue(tableNameList2.get(i));
                long count2 = (long)map2.get("count");
                if(count1 != count2) {
                    errorMap.put(tableNameList1.get(i), count2-count1);
                    errorTable.add(tableNameList1.get(i));
                }

            }
        }
        System.out.println("finish");

    }

    @Override
    public void processCustData() {
        List<CustData> dataList = initExcelData();
        List<CustData> filterList = new ArrayList<>();
        List<CustData> resultList = dataList.stream().filter(data -> StringUtils.hasText(data.getCuscode())).collect(Collectors.toList());
        List<CustData> errorList = dataList.stream().filter(data -> !StringUtils.hasText(data.getCuscode())).collect(Collectors.toList());
        Map<String, List<CustData>> dataMap = resultList.stream().collect(Collectors.groupingBy(CustData::getCuscode));
        dataMap.forEach((key,list)->{
            Set<String> set = new HashSet<>();
            for (CustData custData : list) {
                set.add(custData.getOrgname());
            }
            if(set.size() > 1) {
                filterList.addAll(list);
            }
        });
        writeExcel(filterList,"重复数据");
        writeExcel(errorList,"不完整数据");
        for (CustData custData : filterList) {
            System.out.println(JSON.toJSONString(custData));
        }
    }
    private void writeExcel(List<CustData> dataList,String fileName) {
        String prefixPath = "./";
        String filePath = prefixPath +fileName +".xlsx";
        EasyExcelFactory.write(filePath, CustData.class).sheet().doWrite(dataList);
    }
    @SneakyThrows
    private List<CustData> initExcelData() {
        ClassPathResource classPathResource = new ClassPathResource("cust/");
        File parentFile = classPathResource.getFile();
        File[] files = parentFile.listFiles();
        assert files != null;
        List<CustData> dataList = new ArrayList<>();
        for (File file : files) {
            String fileName = file.getName();
            log.info("读取文件：{} 开始",fileName);
            dataList.addAll(loadFromExcel(file)) ;
            log.info("读取文件：{} 结束",fileName);
        }
        log.info("你好");
        return dataList;
    }
    private List<CustData> loadFromExcel(File file){
        return EasyExcelFactory.read(file).head(CustData.class).sheet().doReadSync();
    }

    private List<String> getTableNameList(List<Map> maps) {
        return maps.stream().map(item -> (String) item.get("TABLE_NAME")).sorted().collect(Collectors.toList());

    }

    private String buildSqlStr(String tableName) {
        return " select count(*) from " + tableName;
    }

}
